package com.system.vote.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.vote.common.utils.StringUtils;
import com.system.vote.admin.entity.SysAccount;
import com.system.vote.admin.entity.SysMenu;
import com.system.vote.admin.entity.SysRole;
import com.system.vote.admin.mapper.SysAccountMapper;
import com.system.vote.admin.mapper.SysMenuMapper;
import com.system.vote.admin.mapper.SysRoleMapper;
import com.system.vote.admin.service.SysAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【sys_account(用户表)】的数据库操作Service实现
 * @createDate 2023-10-21 18:13:02
 */
@Service
public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAccount>
        implements SysAccountService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;

//    @Autowired
//    private SysAccountMapper sysAccountMapper;

    @Override
    public SysAccount getByUserName(String username) {
        return getOne(new QueryWrapper<SysAccount>().eq("username", username).ne("is_delete", "1"));
    }


    @Override
    public String getUserAuthorityInfo(long userId) {
        StringBuffer authority = new StringBuffer();
        //根据用户id获取所有角色信息
        List<SysRole> roleList = sysRoleMapper.selectList(new QueryWrapper<SysRole>().inSql("id", "select role_id from sys_user_role where user_id=" + userId));
        if (roleList.size() > 0) {
            String roleCodeStrs = roleList.stream().map(r -> "ROLE_" + r.getCode()).collect(Collectors.joining(","));
            authority.append(roleCodeStrs);
        }
        //遍历所有的角色，获取所有菜单权限而且不重复
        Set<String> menuCodeSet = new HashSet<>();
        for (SysRole sysRole : roleList) {
            List<SysMenu> menuList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().inSql("id", "select menu_id from sys_role_menu where role_id=" + sysRole.getId()));
            for (SysMenu sysMenu : menuList) {
                String perms = sysMenu.getPerms();
                if (StringUtils.isNotEmpty(perms)) {
                    menuCodeSet.add(perms);
                }
            }
        }
        if (menuCodeSet.size() > 0) {
            authority.append(",");
            String menuCodeStrs = String.join(",", menuCodeSet);
            authority.append(menuCodeStrs);
        }
        System.out.println("authority:" + authority);
        return authority.toString();
    }

    @Override
    public SysAccount getByPhone(String phone) {
        return getOne(new QueryWrapper<SysAccount>().eq("phone", phone).ne("is_delete", "1"));

    }

    @Override
    public SysAccount getByEmail(String email) {
        return getOne(new QueryWrapper<SysAccount>().eq("email", email).ne("is_delete", "1"));

    }
}




